<template>
  <el-dialog
    :title="formData.id ? '编辑配置' : '新增配置'"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="用户ID">
        <span>{{ formData.user_id }}</span>
      </el-form-item>
      <el-form-item label="名称" prop="coupon_name">
        <el-input v-model="formData.coupon_name" />
      </el-form-item>

      <el-form-item label="满多少可用" prop="limit_min_money">
        <el-input v-model="formData.limit_min_money" />
      </el-form-item>

      <el-form-item label="优惠金额" prop="money">
        <el-input v-model="formData.money" />
      </el-form-item>

      <el-form-item label="有效时间" prop="term">
        <el-input v-model="formData.term" placeholder="单位：天" />
      </el-form-item>

      <el-form-item label="开始时间" prop="start_time">
        <el-date-picker v-model="formData.start_time" type="datetime" placeholder="选择时间" />
      </el-form-item>

      <el-form-item label="结束时间" prop="end_time">
        <el-date-picker v-model="formData.end_time" type="datetime" placeholder="选择时间" />
      </el-form-item>

      <el-form-item label="状态" prop="money">
        <el-input v-model="formData.money" placeholder="1: 未使用；2：已使用；-1：已过期；" />
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import lodash from 'lodash'
// const formData = {
//   coupon_name: '',
//   limit_min_money: null,
//   money: null,
//   term: null,
//   start_time: null,
//   end_time: null,
//   status:
// }
import { updateOneCoupon } from '@/api/coupon_batch'
export default {
  name: 'CouponAddEdit',
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      rules: {
        coupon_name: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] }
        ],
        longitude: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[1-9]\d*\.?\d*$/, message: '必须是一个数字', trigger: ['change', 'blur'] }
        ],
        latitude: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[1-9]\d*\.?\d*$/, message: '必须是一个数字', trigger: ['change', 'blur'] }
        ],
        map_scale: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\d+$/, message: '必须是一个整数', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      this.formData = lodash.cloneDeep(row)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    // resetForm() {
    //   this.formData = { ...formData }
    //   this.$refs.addEditForm.resetFields()
    // },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++
          await updateOneCoupon(this.formData)
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}

.new-goods-box,
.activity-goods-box,
.recharge-box {
  li {
    display: flex;
    align-items: center;
  }
  .input {
    margin: 20px;
  }
  .icon {
    cursor: pointer;
    font-size: 30px;
    color: #409eff;
  }
}
</style>
